package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.LoginVo;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: UserInfoServiceImpl
 * Package: com.atguigu.gmall.user.service.impl
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-30 16:04
 * @Version 1.0
 */

@Service
public class UserInfoServiceImpl implements UserInfoService {


 @Autowired
 private UserInfoMapper userInfoMapper;
 /**
  * 单点登录接口
  * @param loginVo
  * @return
  */
 @Override
 public UserInfo login(LoginVo loginVo) {
  // select * from user_info where (login_name = ? or email = ? or phone = ?) and password = ?;
  // userInfoMapper.login(loginVo);
  LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//  userInfoLambdaQueryWrapper.eq(UserInfo::getLoginName,loginVo.getLoginName())
//                  .or().eq(UserInfo::getEmail,loginVo.getLoginName( ))
//                  .or().eq(UserInfo::getPhoneNum,loginVo.getLoginName());
userInfoLambdaQueryWrapper.or(wrapper->{
 wrapper.eq(UserInfo::getLoginName,loginVo.getLoginName())
         .or().eq(UserInfo::getEmail,loginVo.getLoginName( ))
          .or().eq(UserInfo::getPhoneNum,loginVo.getLoginName());
});


  // 加密
  userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd, MD5.encrypt(loginVo.getPasswd()));


  UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
  return userInfo;
 }
}
